﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.Utils;
using DevExpress.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace zmMoniter
{
    public partial class frmUser: DevExpress.XtraEditors.XtraForm
    {
        public mainForm main = null;
        public frmUser(mainForm frm)
        {
            InitializeComponent();
            this.main = frm;



            DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit riButtonEdit = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
            riButtonEdit.TextEditStyle = TextEditStyles.HideTextEditor;
            riButtonEdit.Buttons[0].Kind = ButtonPredefines.Delete;
            riButtonEdit.Buttons[0].Caption = "";

            //riButtonEdit.Buttons[0].Image = ProjectSample.Properties.Resources.PrintPreview;
            riButtonEdit.Buttons[0].ImageAlignment = HorzAlignment.Near;
            riButtonEdit.ButtonClick += new ButtonPressedEventHandler(riButtonEdit_ButtonClick);

            gridCtl_UserList.RepositoryItems.Add(riButtonEdit);
            gridCol_Delete.ColumnEdit = riButtonEdit;
        }
        void riButtonEdit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {

            //取得选定行信息  
            string sID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID").ToString();
            string sUserName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "用户名").ToString();
            DialogResult bDelete = MessageBox.Show("您确认需要删除用户：" + sUserName + "?", "删除用户提示",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (bDelete == DialogResult.Yes)
            {
                if (deleteUser(sID))
                    UpdateUserList();
            }
        }

        private void frmUser1_Load(object sender, EventArgs e)
        {
            UpdateUserList();
        }
        private bool deleteUser(string sID)
        {
            bool bResult = false;

            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            string sSQL = "Delete 用户 WHERE ID=" + sID;
            int iResult = db.ExecuteNonQuery(sSQL);
            if (iResult > 0)
            {
                bResult = true;
            }

            return bResult;
        }
        public bool UpdateUserList()
        {
            bool bRst = false;
            string sSQL = "select 用户.ID,用户.用户名,用户.IP,用户.移动电话,用户.EMAIL," +
            "(case when 用户.是否在线=1 then '在线' else '离线' end) 是否在线," +
            "to_char(用户.登陆时间,'YYYY/MM/DD HH24:MI:SS') AS 登陆时间," +
            "to_char(用户.离线时间,'YYYY/MM/DD HH24:MI:SS') AS 离线时间," +
            "权限.名称 AS 权限 " +
            "FROM 用户 LEFT OUTER JOIN " +
            "权限 ON 用户.权限 = 权限.编号";
            PublicDataCls db = new PublicDataCls();
            string sSQLState = db.OpenConn();
            if (sSQLState != "")
            {
                this.gridCtl_UserList.DataSource = null;

                return bRst;
            }
            DataTable dt = db.ReturnDataTable(sSQL);
            db.CloseConn();

            this.gridCtl_UserList.DataSource = dt;
            bRst = true;
            return bRst;
        }
    }
}